//
//  CYCardListViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/17.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYCardListViewController.h"

#import "CYCardListNoneCell.h"
#import "CYCardListCell.h"

#import "CYBindCardViewController.h"



@interface CYCardListViewController ()

@property(nonatomic, strong)UIButton *addButton;

@property(nonatomic, assign)BOOL loading;

@end


@implementation CYCardListViewController

- (void)close
{
    [MobClick event:UM_EVENT_0065];
    [super close];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadDataFromServer];
}

- (void)loadUI
{
    self.navigationItem.title = @"我的银行卡";
    
    self.tableView.cy_autoLockVertical = YES;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 104)];
    {
        self.addButton = [UIButton cy_mainButtonWithTitle:@"添加银行卡"];
        [self.addButton addTarget:self action:@selector(onAddButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
        self.addButton.hidden = YES;
        [self.tableView.tableFooterView addSubview:self.addButton];
    }
}

- (void)layoutUI
{
    self.tableView.tableFooterView.width = self.tableView.width;
    {
        self.addButton.height = 44;
        self.addButton.width = self.tableView.tableFooterView.width - 40;
        self.addButton.centerY = self.tableView.tableFooterView.height / 2;
        self.addButton.centerX = self.tableView.tableFooterView.width / 2;
    }
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    self.loading = YES;
    if(self.tableModel == nil)
    {
        [SVProgressHUD show];
    }
    @weakify(self);
    [CYStandardDataAPI user_queryBindBankCardWithComplete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            NSMutableArray *cells = [NSMutableArray array];
            dispatch_sync(dispatch_queue_create("com.cyjf.bank-list", DISPATCH_QUEUE_SERIAL), ^{
                if([response.content isKindOfClass:[NSArray class]])
                {
                    NSArray *apiModels = [CYDataAPIBankCardModel mj_objectArrayWithKeyValuesArray:response.content];
                    for(CYDataAPIBankCardModel *apiModel in apiModels)
                    {
                        CYCardListCellObject *cell = [CYCardListCellObject objectWithCellClass:[CYCardListCell class]];
                        cell.apiModel = apiModel;
                        cell.bankIcon = apiModel.bankIcon;
                        cell.bankName = apiModel.bankName;
                        NSString *cardNumber = SAFE_STRING(apiModel.bankCardNo);
                        cell.cardNumber = [@"**** **** **** " stringByAppendingString:cardNumber.length >= 4 ? [cardNumber substringFromIndex:cardNumber.length - 4] : @"****"];
                        
                        NSString *limitTimes = SAFE_STRING(apiModel.limitTimesStr);
                        NSString *limitDay = SAFE_STRING(apiModel.limitDayStr);
                        cell.onceLimit = ([limitTimes isEqualToString:@"0"] || (limitTimes.length == 0)) ? @"单笔无限额" : [NSString stringWithFormat:@"单笔限额%@万", limitTimes];
                        cell.totalLimit = ([limitDay isEqualToString:@"0"] || (limitDay.length == 0)) ? @"单日无限额" : [NSString stringWithFormat:@"单日限额%@万", limitDay];
                        
                        UIColor *leftColor = HEXCOLOR(@"F66F5A");
                        UIColor *rightColor = HEXCOLOR(@"EF4E61");
                        NSString *color = SAFE_STRING(apiModel.bankCardBackground);
                        NSArray *colorArray = [color componentsSeparatedByString:@","];
                        if(colorArray.count == 2)
                        {
                            NSString *leftColorString = colorArray.firstObject;
                            NSString *rightColorString = colorArray.lastObject;
                            if(leftColorString.length == 7 && rightColorString.length == 7)
                            {
                                UIColor *lColor = HEXCOLOR([leftColorString substringFromIndex:1]);
                                UIColor *rColor = HEXCOLOR([rightColorString substringFromIndex:1]);
                                if((![lColor isEqual:[UIColor clearColor]]) && (![rColor isEqual:[UIColor clearColor]]))
                                {
                                    leftColor = lColor;
                                    rightColor = rColor;
                                }
                            }
                        }
                        
                        cell.backgroundImage = [UIImage gradientImageFromColors:@[leftColor, rightColor] ByGradientType:UIImageGradientTypeLeftToRight size:CGSizeMake(SCREEN_WIDTH - 40, 2)];
                        [cells addObject:cell];
                    }
                }
                if(cells.count == 0)
                {
                    NICellObject *cell = [NICellObject objectWithCellClass:[CYCardListNoneCell class]];
                    [cells addObject:cell];
                }
            });
            self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:cells delegate:(id)[NICellFactory class]];
            self.tableView.dataSource = self.tableModel;
            self.addButton.hidden = NO;
            [self.tableView reloadData];
            [SVProgressHUD dismiss];
        }
        else
        {
            if(self.tableModel == nil)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
            }
            else
            {
                [SVProgressHUD dismiss];
            }
        }
        self.loading = NO;
    }];
}

- (void)onReceiveApplicationDidBecomeActiveNotification
{
    [self loadDataFromServer];
}

#pragma mark - Control Events
- (void)onAddButtonTouchUpInside
{
    [MobClick event:UM_EVENT_0066];
    CYBindCardViewController *bindCardViewContoller = [[CYBindCardViewController alloc] init];
    bindCardViewContoller.type = CYBindCardTypeNormal;
    [self.navigationController pushViewController:bindCardViewContoller animated:YES];
}

@end
